﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;
using System.Linq;

namespace MatrixWebApiCore.Common
{
    public class HttpHelper
    {
        /// <summary>
        /// 以get方式请求，同步方式
        /// </summary>
        /// <param name="url"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static string WebClientGet(string url, string token = null)
        {            
            WebClientExtend client = new WebClientExtend();
            client.Timeout = 60 * 3;
            client.Encoding = Encoding.UTF8;
            if (!string.IsNullOrWhiteSpace(token))
            {
                client.Headers.Add("Authorization", token);
            }
            return client.DownloadString(url);
        }
        /// <summary>
        /// 以get方式请求，异步方式
        /// </summary>
        /// <param name="url"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public static async Task<string> WebClientGetAsync(string url, string token)
        {
            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;
            if (!string.IsNullOrWhiteSpace(token))
            {
                client.Headers.Add("Authorization", token);
            }
            return await client.DownloadStringTaskAsync(url);
        }

        /// <summary>
        /// 发送异步请求
        /// </summary>
        /// <param name="url">请求地址</param>
        /// <param name="json">发送的参数json</param>
        /// <param name="token">身份验证字符串</param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static async Task<string> WebClientPostAsync(string url, string json, string token, Dictionary<string, string> headers = null)
        {
            //特殊符号处理，需要编码，"+"编码为%2B
            json = json.Replace("+", "%2B");
            WebClient client = new WebClient();
            client.Headers.Add("Content-Type", "application/json");
            //client.Headers.Add("Content-Type", "application/x-www-form-urlencoded ");
            client.Headers.Add("Authorization", token);
            client.Encoding = Encoding.UTF8;
            if (headers != null)
            {
                foreach (var item in headers)
                {
                    client.Headers.Add(item.Key, item.Value);
                }
            }
            return await client.UploadStringTaskAsync(url, "POST", json);
        }

        /// <summary>
        /// 同步方式，发送请求
        /// </summary>
        /// <param name="url"></param>
        /// <param name="json"></param>
        /// <param name="token"></param>
        /// <param name="headers"></param>
        /// <returns></returns>
        public static string WebClientPost(string url, string json, string token, Dictionary<string, string> headers = null)
        {
            //特殊符号处理，需要编码，"+"编码为%2B
            json = json.Replace("+", "%2B");
            WebClientExtend client = new WebClientExtend();
            client.Timeout = 60 * 3;
            client.Headers.Add("Content-Type", "application/json");
            //client.Headers.Add("Content-Type", "application/x-www-form-urlencoded ");
            if (!string.IsNullOrWhiteSpace(token))
            {
                client.Headers.Add("Authorization", token);
            }
            //client.
            client.Encoding = Encoding.UTF8;
            if (headers != null)
            {
                foreach (var item in headers)
                {
                    client.Headers.Add(item.Key, item.Value);
                }
            }
            return client.UploadString(url, "POST", json);
        }


    }
}